package vmcs.simulationsystem;

import java.awt.*;
import vmcs.*;
import vmcs.customer.CoinReceiver;
import vmcs.store.Coin;
import vmcs.store.StoreItem;

public class CoinInputBox extends Panel {

	private CoinInputListener coinInputListener;
	public final static Color ACT_COLOR = Color.white;

	private double[] weights;
	private CoinReceiver coinReceive;
	private Button[] bn;
	private int i = 0;
	private Integer[] coin_weights;

	public CoinInputBox(Container composite, CoinReceiver coinRcv) {
		coinReceive = coinRcv;
		this.setLayout(new BorderLayout());

		Label lb = new Label("Enter Coins Here");
		lb.setFont(new Font("Helvetica", Font.BOLD, 12));
		lb.setAlignment(Label.LEFT);
		Panel lp = new Panel(new GridLayout(1, 1));
		lp.add(lb);

		StoreItem[] items = coinReceive.getCashStore();
		Panel tp = new Panel(new GridLayout(1, items.length + 1));
		weights = new double[items.length + 1];
		bn = new Button[items.length + 1];
		for (i = 0; i < items.length; i++) {
			Coin ob = (Coin) items[i].getContent();
			weights[i] = ob.getWeight();
			bn[i] = new Button(ob.getName());
			bn[i].setBackground(ACT_COLOR);
			bn[i].setFont(new Font("Helvetica", Font.BOLD, 14));
			tp.add(bn[i]);
		}
		bn[i] = new Button("Invalid");
		weights[i] = 0;
		bn[i].setFont(new Font("Helvetica", Font.BOLD, 14));
		bn[i].setBackground(ACT_COLOR);
		tp.add(bn[i]);
		this.add("North", lp);
		this.add("Center", tp);
	}

	/**
	 * This operation attach listener and set action command to Button objects.
	 */
	public void addListener() {
		for (i = 0; i < bn.length; i++) {
			bn[i].addActionListener(new CoinInputListener(coinReceive));
			bn[i].setActionCommand(String.valueOf(weights[i]));
		}
	}

	public void setActive(boolean active) {
		for(Button btn : bn){
			btn.setEnabled(active);
		}
	}
}
